// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import QtMultimediaKit 1.1
import com.nokia.meego 1.0

Page {
    orientationLock: PageOrientation.LockLandscape
    property string lastCapturedImage
    signal imageSelected (string image)

    function handleCamera() {
        if (visible && Qt.application.active) {
            console.log("camera start")
            camera.start()
        } else {
            console.log("camera stop")
            camera.stop()
        }

    }

    onVisibleChanged: handleCamera()
    Connections {
        target: Qt.application
        onActiveChanged: handleCamera()
    }

    Rectangle {
        anchors.fill: parent
        rotation: screen.currentOrientation == 1 ? 90 : 0
        color: "black"

        Camera {
            id: camera
            focus : visible // to receive focus and capture key events when visible
            anchors.fill: parent

            flashMode: Camera.FlashAuto
            whiteBalanceMode: Camera.WhiteBalanceAuto
            captureResolution : "1024x768"

            onImageCaptured: { photoPreview.source = preview }
            onImageSaved: { lastCapturedImage = path }
        }
    }
    Button {
        id: captureButton
        text: qsTr("Capture")
        anchors {
            right: parent.right
            bottom: parent.bottom
            margins: UiConstants.DefaultMargin
        }
        onClicked: {
            camera.captureImage()
            photoPicker.open()
        }
    }

    Sheet {
        id: photoPicker
        acceptButtonText: qsTr("Save")
        rejectButtonText: qsTr("Cancel")

        content: Rectangle {
            color: "black"
            anchors.fill: parent
            Image {
                id: photoPreview
                rotation: screen.currentOrientation == 1 ? 90 : 0
                anchors.fill: parent
                fillMode: Image.PreserveAspectFit
                smooth: true
            }
        }

        onAccepted: {
            imageSelected(lastCapturedImage)
            appWindow.pageStack.pop()
        }
        onRejected: {
            rosYama.removeFile(lastCapturedImage)
            lastCapturedImage = ""
        }
    }
}
